Skip to content

fix(studio): open pages on sites with trailingSlash: 'always' - #109

Open
rubenkalath wants to merge 1 commit into
flowtricks:mainfrom
rubenkalath:fix/studio-trailing-slash-always
Open

rubenkalath wants to merge 1 commit into
flowtricks:mainfrom
rubenkalath:fix/studio-trailing-slash-always

Conversation

@rubenkalath

Copy link
Copy Markdown

Problem

On an Astro site with trailingSlash: 'always', Studio opens the home page, but every other page (plain pages, collection templates) stays on "Your preview is taking a little longer to start… The page loaded, but its editor script has not answered."

Two causes:

  1. Studio names pages by slashless routes (routeFor → /about). Both sendPreviewURL (studio/src/server/integration.ts) and the hosted previewEntry redirect (studio/src/cloudflare/worker.ts) send the canvas there as is. Under 'always', Astro's dev server answers /about with its 404 page, which has no editor script.
  2. Once the frame reaches /about/, usePreviewLoading compares the path the client reports (/about/) with pagePath(url) (/about) using ===, so the canvas keeps waiting.

Desktop doesn't have the problem: it reads trailingSlash and applies it in routeToPath. Studio's project scan has no trailingSlash.

To reproduce: install Studio into any site with trailingSlash: 'always', run npm --prefix studio run dev, open /studio, and pick any page except Home.

Fix

  • studio/src/server/pageNavigation.ts (new): slashRedirect() returns the spelling Astro serves (/about → /about/, query kept) for a page navigation that is missing the slash on an 'always' site. It only applies to Sec-Fetch-Mode: navigate with GET or HEAD.
    • Never touched: files (/feed.xml), Studio paths (/studio/…, /__avb/…, /__studio-entry), Vite/Astro paths (/@…, /_astro/…), fetches and writes.
    • 'never' needs nothing, because Studio routes never end in a slash.
  • integration.ts: the studioRequests middleware answers that navigation with a 302. The container is the one place that knows the site's setting in both hosts (local dev, and the Worker proxying the frame), so neither preview-url nor previewEntry has to learn it. The fragment (#avb-design) survives the redirect.
  • previewHandshake.ts: samePage(reported, asked) treats /about/ and /about as one page (the query must still match). PreviewLoading.tsx uses it for the ready check.
  • studio/test/pageNavigation.test.ts: covers each setting, files, tooling paths, fetches and writes, the assertion, and samePage.

An alternative is the Desktop approach: add trailingSlash to Studio's scan and apply routeToPath in the client before asking for preview-url. It touches more files (the scan, template entry routes, the switcher's comparisons) and still needs samePage. Happy to switch if you prefer it.

Checks

  • tsc --noEmit, ESLint and Prettier on the changed files.
  • tsx --test test/pageNavigation.test.ts test/previewHandshake.test.ts: 16 pass.
  • Manually on a production site (Astro 7.3, trailingSlash: 'always'): a page, a second page, a blog post template and a case study template open on the canvas and save edits.

I did not run the full npm test gate.

Studio sent the canvas to slashless routes (/about), which Astro's dev
server answers with a 404 when the site sets trailingSlash: 'always', so
every page but the home page stayed on "editor script has not answered".
The container now redirects a slashless page navigation to the spelling
Astro serves, and the ready check treats /about/ and /about as one page.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant